package com.ficus.road.maintain.converter;

import com.ficus.road.maintain.core.model.Car;
import com.ficus.road.maintain.core.model.Hit;
import com.ficus.road.maintain.core.model.SyncHitResponse;
import com.ficus.road.maintain.dao.CarDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SyncHitConverter {

    @Autowired
    private CarDao carDao;

    public SyncHitResponse convert(Hit hit) {
        SyncHitResponse syncHit = new SyncHitResponse();
        syncHit.setId(hit.getId());
        String frameImageUrl = hit.getAttrs().getFrameImageUrl();
        if (StringUtils.isBlank(frameImageUrl)) {
            frameImageUrl = hit.getAttrs().getSceneImage();
        }
        syncHit.setImageUrl(frameImageUrl);
        syncHit.setType(hit.getType());
        syncHit.setTime(hit.getFirstHitTime());
        syncHit.setLat(String.valueOf(hit.getAttrs().getGpsLocation().getLatitude()));
        syncHit.setLgt(String.valueOf(hit.getAttrs().getGpsLocation().getLongitude()));
        syncHit.setRoadName(hit.getAttrs().getRoadName());
        Car car = carDao.fetch(hit.getAttrs().getCarId());
        syncHit.setPlateNo(car.getPlateNo());
        return syncHit;
    }
}
